package 剑指offer;

/**
 * @description:
 * @author: ywk
 * @date: 2021-04-23
 */
public class 矩阵元素查找 {
    //通过二分查找
    public static int[] findElement1(int[][] mat, int n, int m, int x) {
        // write code here
        int lowN = 0;
        int lowM = 0;
        int highN = n;
        int highM = m;
        while (mat[lowN][lowM] != x) {
            int midN = lowN + (highN - lowN) / 2;
            int midM = lowM + (highM - lowM) / 2;
            if (mat[midN][midM] < x) {

            }
        }
        System.out.println();
        return new int[3];
    }

    public static int[] findElement(int[][] mat, int n, int m, int x) {
        // write code here
        if (n == 0 || m == 0)
            return new int[0];
        int[] re = null;
        int col = n - 1;
        int row = 0;
        while (col < mat.length && row >= 0) {
            if (mat[col][row] == x) {
                re = new int[2];
                re[0] = col;
                re[1] = row;
                break;
            } else if (mat[col][row] < x) {
                row++;
            } else {
                col--;
            }
        }
        if (re == null) {
            return new int[0];
        } else {
            return re;
        }
    }

    public static void main(String[] args) {
        findElement(new int[][]{{1, 2, 3}, {4, 5, 6}}, 2, 3, 6);
    }
}
